package playsManager.gui;

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

import playsManager.*;
import playsManager.domainLogic.*;
import playsManager.utilities.Utilities;

class AdminMenu extends UserMenu implements ActionListener, TableModelListener
{
	AdminLogon admin;

	protected JDialog dialog;
	protected JButton viewPlays;
	protected JButton commit;
	protected JButton cancel;
	protected JButton addPlay;
	protected JTextField title;
	protected JTextField description;
	protected JPanel p;

	public AdminMenu(AdminLogon admin, DomainLogic theDl)
	{
		super(theDl);

		this.admin = admin;
	}

	@Override
	protected void InitMenu() {

		//add controls to viewer menu
		p = new JPanel();
		viewPlays = new JButton("View Theaters");
		viewPlays.addActionListener(this);
		commit = new JButton("Add new Theater");
		commit.addActionListener(this);
		p.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		menuFrame.add(p);
		p.add(viewPlays);
		p.add(commit);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		if(arg0.getSource().equals(viewPlays))
		{
			menuFrame.removeAll();
			p.removeAll();
			String[] columnNames = {"Theater", "ID", "Description"};
			ArrayList<Theater> theTheaters;
			try 
			{
				theTheaters = dl.GetAllTheaters();
			
				Object[][] data = new String[theTheaters.size()][4];
				for(int i = 0; i < theTheaters.size(); i++){
					data[i][0] = theTheaters.get(i).name;
					data[i][1] = theTheaters.get(i).id;
					data[i][2] = theTheaters.get(i).description;
					data[i][3] = new Boolean(false);
				}
				JTable t = new JTable(data, columnNames);
				t.getModel().addTableModelListener(this);
				dialog = new JDialog(menuFrame, "Plays", true);
				dialog.add(t);
			}
			catch (Exception e) 
			{
				Utilities.ShowErrorMessage("error loading data from database.");
				e.printStackTrace();
			}
			dialog.setVisible(true);
			//menuFrame.add(p);
		}
		else if(arg0.getSource().equals(commit)){
			//menuFrame.removeAll();
			p.removeAll();
			addPlay = new JButton("OK");
			cancel = new JButton("Cancel");
			addPlay.addActionListener(this);
			cancel.addActionListener(this);
			title = new JTextField("Theater Name");
			description = new JTextField("Theater Description");
			p.add(title);
			p.add(description);
			p.add(addPlay);
			p.add(cancel);
			dialog = new JDialog(menuFrame, "Add New Theater", true);
			dialog.setSize(400, 200);
			dialog.add(p);
			dialog.setVisible(true);
			//menuFrame.add(p);
		}
		else if(arg0.getSource().equals(cancel)){
			dialog.setVisible(false);
		}
		else if(arg0.getSource().equals(addPlay)){
			String titleString, descriptionStyle;
			if(title.getText().compareTo("Play Title") == 0 || title.getText().compareTo("Must have a title!") == 0 || title.getText().compareTo("") == 0){
				title.setText("Must have a title!");
			}
			else{
				titleString = title.getText();
				if(description.getText().compareTo("Play Description") == 0){
					descriptionStyle = "A play";
				}
				else{
					descriptionStyle = description.getText();
				}
				Theater theTheater = new Theater();
				theTheater.description = descriptionStyle;
				theTheater.name = titleString;

				dialog.setVisible(false);
			}
		}
	}

	@Override
	public void tableChanged(TableModelEvent arg0) {

		if(arg0.getColumn() == 3){
			try {
				dl.DeleteTheater(dl.GetAllTheaters().get(arg0.getFirstRow()).id);
			} catch (Exception e) {
				Utilities.ShowErrorMessage("error deleting theater.");
				e.printStackTrace();
			}
		}
	}
}
